/*
 * @Author: lsmi
 * @Date: 2021-07-11 20:28:29
 * @LastEditors: lsmi
 * @LastEditTime: 2021-10-28 20:45:21
 * @FilePath: \v2_play_js\vue.config.js
 */
const path = require("path");
const CompressionWebpackPlugin = require("compression-webpack-plugin");
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;

// const TerserPlugin = require('terser-webpack-plugin')
function resolve(dir) {
  return path.join(__dirname, dir);
}
module.exports = {
  devServer: {
    proxy: {
      "/api": {
        //这里最好有一个 /
        target: "http://122.9.106.197:8089", // 后台接口域名
        secure: false, // 如果是https接口，需要配置这个参数
        changeOrigin: true, //是否跨域
        pathRewrite: {
          "^/api": "",
        },
      },
    },
  },
  chainWebpack: (config) => {
    // common: require('path').resolve(__dirname, '../src/common')
    // 配置使用简单名称
    config.resolve.alias
      .set("@/views", resolve("src/views"))
      .set("@/store", resolve("src/store"))
      .set("@/utils", resolve("src/utils"))
      .set("@/config", resolve("src/config"))
      .set("@/routes", resolve("src/routes"))
      .set("@/assets", resolve("@/assets"))
      .set("@/service", resolve("src/service"))
      .set("@/plugins", resolve("src/plugins"))
      .set("@/components", resolve("src/components"))
      .set("@/directives", resolve("src/directives"))
      .set("@/filters", resolve("src/filters"))
      .set("@/mixins", resolve("src/mixins"))
      .set("@", resolve("./src"));
    config.optimization.namedChunks(true).splitChunks({
      minSize: 30000,
      cacheGroups: {
        commons: {
          chunks: "initial", // 'initial', 'async', 'all'
          name: "commons",
          minChunks: 2,
          maxInitialRequests: 5,
          minSize: 0,
          priority: 0, // 优先级
        },
        // 单独打包vue插件
        "vue-vendor": {
          chunks: "initial", // 'initial', 'async', 'all',
          // eslint-disable-next-line no-useless-escape
          test: /[\/]node_modules[\/]vue/, // <- window | mac -> /node_modules/vue/
          name: "vue-vendor",
          priority: -9,
          enforce: true,
        },
        vendor: {
          chunks: "initial", // 'initial', 'async', 'all'
          test: /node_modules/,
          // test: /node_modules/,
          name: "vendor",
          priority: -10,
          enforce: true,
        },
      },
    });
    // 处理打包后不显示console
    config.optimization.minimizer("terser").tap((options) => {
      // false是显示  true是不显示
      options[0].terserOptions.compress.drop_console = false;
      return options;
    });
  },
  configureWebpack: (config) => {
    const plugins = [];
    // start 生成 gzip 压缩文件
    plugins.push(
      new CompressionWebpackPlugin({
        filename: "[path].gz[query]", //目标资源名称
        algorithm: "gzip",
        test: productionGzipExtensions, //处理所有匹配此 {RegExp} 的资源
        threshold: 10240, //只处理比这个值大的资源。按字节计算(楼主设置10K以上进行压缩)
        minRatio: 0.8, //只有压缩率比这个值小的资源才会被处理
      })
    );

    // End 生成 gzip 压缩文件
    config.plugins = [...config.plugins, ...plugins];
  },
};
